<template>
  <div id="app">
    <el-card shadow="always">
<!--        条件查询带分页-->
        <el-form :inline="true" size="medium" :model="studentSearch" class="demo-form-inline">
          <el-form-item>
            <el-input v-model="studentSearch.sname" placeholder="学生姓名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="studentSearch.addr" placeholder="学生住址"></el-input>
          </el-form-item>
          <el-form-item>
            <el-select v-model="studentSearch.cid" placeholder="所在班级">
              <el-option label="所有班级" value="0"></el-option>
              <el-option :label="c.cname" :value="c.cid" v-for="c in classesList" :key="c.cid"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search"  plain icon="el-icon-search">查询</el-button>
          </el-form-item>
        </el-form>
        <el-table
            :data="studentList"
            border
            style="width: 100%">
          <el-table-column
              label="学号"
              align="center"
              prop="sid"/>
          <el-table-column
              label="姓名"
              align="center"
              prop="sname"/>
          <el-table-column
              label="性别"
              align="center"
              prop="sex"/>
          <el-table-column
              label="年龄"
              align="center"
              prop="age"/>
          <el-table-column
              label="地址"
              align="center"
              prop="addr"/>
          <el-table-column
              label="所在班级"
              align="center"
              prop="cname"/>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button size="mini"
                             @click="handleClick(scope.$index,scope.row)">编辑</el-button>
              <el-button type="danger" size="mini" @click="handleClick(scope.$index,scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
<!--分页-->
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="page"
              :page-sizes="[5, 10, 15, 20]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
          </el-pagination>
    </el-card>
  </div>
</template>

<script>
import studentApi from "@/api/student";
import classesApi from "@/api/classes";

export default {
  data() {
    return {
      studentList: [{}], //当前页的所有学生信息集合
      page: 1,  //当前页
      pageSize: 5, //每页显示记录条数
      total: 100,  //总记录数
      studentSearch:{},  //查询条件对象
      classesList: [],  //所有班级
    }
  },
  created() {
    //this.findAllStudents()
    //this.findAllClasses()
    this.search()
    this.findAllClasses()
  },
  methods: {
    //1.查找所有学生
    async findStudents(){
      const {data:res} = await  studentApi.findAllStudents()
      console.log(res.data)
      if (res.code === 200){
        this.studentList = res.data
      }
    },
    //2.分页查询
    async findByPage(){
      const {data:res} = await studentApi.findByPage(this.page,this.pageSize)
      console.log("分页数据:",res)
      if (res.code === 200){
        this.studentList = res.data.pr.row
        this.total = res.data.pr.total
      }
    },
    // 3.查询所有班级
    async findAllClasses(){
      const {data:res} = await classesApi.findAllClasses()
      console.log(res)
      if (res.code === 200){
        this.classesList = res.data.list
      }
    },
    // 4. 条件查询带分页
    async search(){
      const {data:res} = await studentApi.search(this.page,this.pageSize,this.studentSearch)
      console.log("分页数据:",res)
      if (res.code === 200){
        this.studentList = res.data.pr.row
        this.total = res.data.pr.total
      }
    },
    handleClick(row) {
      console.log(row);
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    //当pageSize大小发生变化时触发
    handleSizeChange(val) {
      this.studentList = val;
      this.search();
      console.log(`每页 ${val} 条`);
    },
    //当前页码发生变化时触发
    handleCurrentChange(val) {
      this.page = val;
      this.search();
      console.log(`当前页: ${val}`);
    },

    },

}
</script>

<style>
  #app{
    display: flex;
    justify-content: center;
  }
  .el-card{
    width: 90%;
  }
  .el-pagination{
    margin-top: 20px;
    text-align: right;
  }
</style>
